/**
 * packges for all elements of the controller
 */
package control;

import java.awt.Color;

import model.BoardModel;
import model.GameColor;
import view.BoardView;

/**
 * Kamisdo is a game like chess. This is the main class. The ki works with
 * alpha-beta pruning. The architectural pattern is model-view-controller.
 * 
 * @author Dizzy
 */
public class Kamisado {

	/**
	 * board color pattern
	 */
	private static GameColor[][] colors;

	public static final Color PlayerBlack = Color.BLACK;

	public static final Color PlayerWhite = Color.WHITE;

	/**
	 * returns the color for a field of the board
	 * 
	 * @param x
	 *            the horizontal position of a field from left to right
	 * @param y
	 *            the vertical position of a field from up to down
	 * @return color from field (x,y)
	 */
	public static GameColor getColor(int x, int y) {
		return colors[x][y];
	}

	/**
	 * just prints start and end to console and start one instance of the game
	 * 
	 * @param args
	 *            not in use yet
	 */
	public static void main(String[] args) {
		// create one instance of the game
		new Kamisado();
		// k.startGame();
	}

	public static void printColorPattern() {
		System.out.println("0\t0\t0\t0\t0\t0\t0\t0\t0\t0");
		for (int i = 0; i < colors.length; i++) {
			System.out.print("0\t");
			for (int j = 0; j < colors[i].length; j++) {
				System.out.print(colors[i][j].getCode() + "\t");
			}
			System.out.print("0");
			System.out.println();
		}
		System.out.println("0\t0\t0\t0\t0\t0\t0\t0\t0\t0");
	}

	/**
	 * main model class to represent the gameboard situation
	 */
	private BoardModel model;

	/**
	 * player with startposition in north
	 */
	private Player playerBlack;

	/**
	 * player with startposition in south
	 */
	private Player playerWhite;

	/**
	 * main view class for the swing gui
	 */
	private BoardView view;

	/**
	 * main contructor of the game, initiates a model (BoardM), view (Board) and
	 * connects each other, this class self is a part of the controller
	 */
	public Kamisado() {
		setColors();
		model = new BoardModel(this);
		view = new BoardView(model, this);
	}

	public void addLog(String s) {
		view.addLog(s);
	}

	public void clicked(Pos pos) {

		// first action, set mark
		if (!model.isGameOver() && model.getTurn() != null && !model.isMarked()
				&& model.getFigure(pos) != null
				&& model.getTurn() == model.getFigure(pos).getPlayer()) {

			model.setMarked(pos);
			view.updateStatus();

			// falls erster Zug von KI, dann fordere Ziel der markierten Figur
			// explizit an
			if (model.getTurn().isAI())
				doNextMove();

			// moving
		} else if (!model.isGameOver()
				&& model.getTurn() != null
				&& model.isMarked()
				&& model.getMarkedField().getFigure().getPlayer() == model
						.getTurn()) {

			model.move(pos);
			view.updateStatus();

		}

	}

	public void doNextMove() {
		Pos pos = model.getTurn().getNextMove();
		clicked(pos);
	}

	public Player getBlackPlayer() {
		return playerBlack;
	}

	public Player getWhitePlayer() {
		return playerWhite;
	}

	public void newGameCvC() {
		playerWhite = new PlayerAI1("Peter", PlayerWhite, model);
		playerBlack = new PlayerAI2("Hans", PlayerBlack, model);
		startGame();
	}

	public void newGameHvC() {
		playerWhite = new PlayerGUI("Peter", PlayerWhite, model, view);
		playerBlack = new PlayerMinMax("Hans", PlayerBlack, model);
		startGame();
	}

	public void newGameHvH() {
		playerWhite = new PlayerGUI("Peter", PlayerWhite, model, view);
		playerBlack = new PlayerGUI("Hans", PlayerBlack, model, view);
		startGame();
	}

	public void nextMove(Player p) {
		model.move(p.getNextMove());
	}

	/**
	 * declarates the colors array with the board color pattern from Kamisado
	 */
	private void setColors() {
		// declare custom colors

		GameColor orange = new GameColor(238, 118, 0, "Orange", 1);
		GameColor blue = new GameColor(Color.BLUE, "Blue", 2);
		GameColor purple = new GameColor(160, 32, 240, "Purple", 3);
		GameColor violet = new GameColor(238, 130, 238, "Violet", 4);
		GameColor yellow = new GameColor(Color.YELLOW, "Yellow", 5);
		GameColor red = new GameColor(Color.RED, "Red", 6);
		GameColor green = new GameColor(Color.GREEN, "Green", 7);
		GameColor brown = new GameColor(139, 69, 19, "Brown", 8);

		// create board color pattern array
		GameColor[][] cols = {
				{ orange, blue, purple, violet, yellow, red, green, brown },
				{ red, orange, violet, green, blue, yellow, brown, purple },
				{ green, violet, orange, red, purple, brown, yellow, blue },
				{ violet, purple, blue, orange, brown, green, red, yellow },
				{ yellow, red, green, brown, orange, blue, purple, violet },
				{ blue, yellow, brown, purple, red, orange, violet, green },
				{ purple, brown, yellow, blue, green, violet, orange, red },
				{ brown, green, red, yellow, violet, purple, blue, orange } };
		colors = cols;

	}

	public void startGame() {
		model.reset();
		model.setTurn(playerWhite);
		view.updateStatus();
	}
}
